<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function validate($request, $rules, $message)
    {
        $validator = Validator::make($request->all(), $rules, $message);
        if ($validator->fails()) {
            foreach (json_decode(json_encode($validator->errors()), true) as $k => $v) {
                return $v[0];
            }
        }
        return null;
    }

    protected function success($msg, $data = [], $skip = null)
    {
        return json_encode([
            'code' => '2000',
            'msg' => $msg,
            'data' => $data,
            'skip' => $skip,
        ]);
    }

    protected function error($msg, $data = [], $skip = null)
    {
        return json_encode([
            'code' => '5000',
            'msg' => $msg,
            'data' => $data,
            'skip' => $skip,
        ]);
    }
}
